package com.bookmall.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bookmall.common.constant.ExceptionTypeConstants;
import com.bookmall.common.constant.MyStatusConstants;
import com.bookmall.common.utils.PageUtils;
import com.bookmall.product.domain.BookInfo;
import com.bookmall.product.domain.vo.BookInfoVo;
import com.bookmall.product.domain.vo.BookWareInfoVo;
import com.bookmall.product.domain.vo.LockStockResult;
import com.bookmall.product.domain.vo.WareBookLockVo;
import com.bookmall.product.service.IBookInfoService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 上架书籍信息 Controller
 * @Author 胡菜鸡
 * @Create 2022-02-18-15:25
 */
@RestController
@RequestMapping("/product/bookInfo")
public class BookInfoController extends BaseController {

    @Autowired
    private IBookInfoService bookInfoService;

    /**
     * 库存锁定
     * @author 胡菜鸡
     * @date 2022/4/25 9:12
     */
    @PostMapping("/lock/order")
    public AjaxResult orderLockStock(@RequestBody WareBookLockVo wareBookLockVo){
        try {
            bookInfoService.orderLockStock(wareBookLockVo);
            return AjaxResult.success();
        } catch (ServiceException e) {
            e.printStackTrace();
            return AjaxResult.error("商品库存不足", ExceptionTypeConstants.PRODUCT_WARE_NO_STOCK);
        }
    }

    /**
     * 查询上架书籍信息列表（分页可带条件查询）
     */
    @PreAuthorize("@ss.hasPermi('product:bookInfo:list')")
    @Log(title = "查询列表", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public AjaxResult list(BookInfoVo bookInfoVo)
    {
        PageUtils page = bookInfoService.selectBookInfoList(bookInfoVo);
        return AjaxResult.success(page);
    }

    /**
     * 导出书籍信息列表
     * @author 胡菜鸡
     * @date 2022/2/20 19:17
     */
    @PreAuthorize("@ss.hasPermi('product:bookInfo:export')")
    @Log(title = "导出书籍信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BookInfo bookInfo)
    {
        List<BookInfo> list = bookInfoService.list(new QueryWrapper<>(bookInfo));
        ExcelUtil<BookInfo> util = new ExcelUtil<BookInfo>(BookInfo.class);
        util.exportExcel(response, list, "书籍信息数据");
    }

    /**
     * 获取书籍详细信息
     * @author 胡菜鸡
     * @date 2022/2/20 18:17
     */
    @PreAuthorize("@ss.hasPermi('product:bookInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(bookInfoService.selectBookInfoById(id));
    }

    /**
     * 新增书籍
     * @author 胡菜鸡
     * @date 2022/2/20 17:49
     */
    @PreAuthorize("@ss.hasPermi('product:bookInfo:add')")
    @Log(title = "新增书籍", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BookInfoVo bookInfoVo)
    {
        return toAjax(bookInfoService.insertBookInfo(bookInfoVo));
    }

    /**
     * 修改书籍信息
     * @author 胡菜鸡
     * @date 2022/2/20 18:52
     */
    @PreAuthorize("@ss.hasPermi('product:bookInfo:edit')")
    @Log(title = "修改书籍", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BookInfoVo bookInfoVo)
    {
        return toAjax(bookInfoService.updateBookInfo(bookInfoVo));
    }

    /**
     * 批量修改书籍锁定库存信息
     * @author 胡菜鸡
     * @date 2022/4/26 14:45
     */
    @PreAuthorize("@ss.hasPermi('product:bookInfo:edit')")
    @Log(title = "批量修改书籍锁定库存信息", businessType = BusinessType.UPDATE)
    @PutMapping("/lock")
    public AjaxResult editLock(@RequestBody List<BookWareInfoVo> bookWareInfoVo)
    {
        return toAjax(bookInfoService.updateBookLockCountBatchById(bookWareInfoVo));
    }

    /**
     * 修改书籍上架状态
     * @author 胡菜鸡
     * @date 2022/2/20 18:52
     */
    @PreAuthorize("@ss.hasPermi('product:bookInfo:edit')")
    @Log(title = "书籍上架状态", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody BookInfo bookInfo)
    {
        return toAjax(bookInfoService.updateBookStatus(bookInfo));
    }

    /**
     * 上架书籍
     */
    @PreAuthorize("@ss.hasPermi('product:bookInfo:up')")
    @Log(title = "上架书籍", businessType = BusinessType.INSERT)
    @PostMapping("/{id}/up")
    public AjaxResult bookUp(@PathVariable("id") String id)
    {
        return AjaxResult.success(bookInfoService.up(id, MyStatusConstants.BOOK_UP));
    }

    /**
     * 下架书籍
     * @author 胡菜鸡
     * @date 2022/4/29 17:42
     */
    @PreAuthorize("@ss.hasPermi('product:bookInfo:up')")
    @Log(title = "上架书籍", businessType = BusinessType.INSERT)
    @PostMapping("/{id}/down")
    public AjaxResult bookDown(@PathVariable("id") String id)
    {
        return AjaxResult.success(bookInfoService.up(id, MyStatusConstants.BOOK_DOWN));
    }

    /**
     * 删除书籍信息
     * @author 胡菜鸡
     * @date 2022/2/20 19:17
     */
    @PreAuthorize("@ss.hasPermi('product:bookInfo:remove')")
    @Log(title = "删除书籍", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(bookInfoService.deleteBookInfoByIds(ids));
    }
}
